var express = require('express');
var router = express.Router();
let crypto = require("crypto");//加密
let jwt = require("jsonwebtoken");
let userService = require("../service/userinfoService");

/* GET users listing. */
router.get('/', function (req, res, next) {
  res.send('respond with a resource');
});

router.post("/login", async (req, res, next) => {
  let { username, userpwd } = req.body;//接收接口调用 传来的数据
  let md5 = crypto.createHash("md5");
  userpwd = md5.update(userpwd).digest("hex");//加密
  let result = await userService.login({ username, userpwd });
  if (result.length) {
    let info = result[0];
    let token = jwt.sign({
      userid: info._id,
      username: info.username,
      usertype: info.usertype,
    }, '2215', { expiresIn: 1000 * 60 * 30 })
    res.json({
      code: 200,
      msg: "登录成功",
      token,
      menu: info.usertype == 1 ?
        [{ path: 'teacherLeave', name: 'teacherLeave', component: 'TeacherLeave', msg: '请假列表' }, { path: 'student', name: 'student', component: 'StudentList', msg: '学员管理' }, { path: 'echarts', name: 'echarts', component: 'Echarts', msg: '请假统计' }] : [{ path: 'studentLeave', name: 'studentLeave', component: 'StudentLeave', msg: '学生请假' }]
    })
  } else {
    res.json({
      code: 500,
      msg: "用户或密码错误"
    })
  }


})
router.post("/registe", async (req, res, next) => {
  // let { username, userpwd = '123123', type = 2 } = req.body; //接收数据
  let token = req.headers.token;
  //接收并查验token权限
  jwt.verify(token, '2215', async (err, info) => {
    if (err || info.usertype != 1) {
      res.json({
        code: 201,
        msg: '权限异常'
      })
    } else {
      //查验成功获取传入的数据集合
      let { list } = req.body;
      //[{姓名: 'demo1'},{姓名: 'demo2'}];
      //将传入集合按照指定格式转化为数组
      let usernameList = list.map((item) => item["姓名"]); //["demo1","demo2","demo3"]
      //查验是否存在相同名称的账号
      let result = await userService.getuserlist({ username: { $in: usernameList } });
      if (result.length) {
        //如果存在重名账号
        res.json({
          code: 203,
          msg: "有存在的用户名",
          list: result.map((item) => item.username),
        });
      } else {
        // 没有重名用户
        let md5 = crypto.createHash("md5");
        let userpwd = md5.update('123123').digest('hex');//加密操作
        let type = 2;//默认类别
        let newList = usernameList.map((username) => ({ username, userpwd, usertype: type }));
        //把数据写入数据库(mongodb)
        let result = await userService.registe(newList)
        res.json({
          code: 200,
          msg: '注册成功',
          list: result
        })
      }
    }
  })
})
router.get('/getuserlist', async (req, res, next) => {
  let token = req.headers.token;
  let params = req.query;
  jwt.verify(token, '2215', async (err, info) => {
    if (err || info.usertype != 1) {
      res.json({
        code: 201,
        msg: 'token解析异常'
      })
    } else {
      let result = await userService.getuserlist(params);
      res.json({
        code: 200,
        msg: '获取成功',
        list: result,
        params
      })
    }
  })
})
module.exports = router;
